package com.wontube.smr.cron;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wontube.core.cron.IJobs;
import com.wontube.smr.bo.sites.moviefone.MovieFoneObtainBO;

/**
 * @author tanggf(2012-7-31)
 *
 */
public class ObtainMoviefoneJobs implements IJobs{
	private final static Logger logger = LoggerFactory.getLogger(ObtainMoviefoneJobs.class);

	@Override
	public String name() {
		return "obtain-moviefone";
	}

	@Override
	public IJobs execute(String[] args) {
		Set<String> set = new HashSet<String>();
		for (String arg : args) {
			set.add(arg);
		}
		
		if(set.contains("-theater")){
			this.obtainTheaterMovies();
		}
		
		if(set.contains("-coming")){
			this.obtainComingsoonMovies();
		}
		
		if(set.contains("-chart")){
			this.obtainMovieCharts();
		}
		
		return this;
	}
	
	private void obtainTheaterMovies(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = MovieFoneObtainBO.saveTheaterMovies();
		} catch (Exception e) {
			logger.error("errors happen when grab moviefone in-theater movies: ", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} moviefone in-theater movies, cost {} s", count, cost);		
	}
	
	private void obtainComingsoonMovies(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = MovieFoneObtainBO.saveComingsoonMovies();
		} catch (Exception e) {
			logger.error("errors happen when grab moviefone coming soon movies: ", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} moviefone coming soon movies, cost {} s", count, cost);		
	}
	
	private void obtainMovieCharts(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = MovieFoneObtainBO.obtainMovieChart();
		} catch (Exception e) {
			logger.error("errors happen when grab yahoo movie chart: ", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} yahoo movie chart, cost {} s", count, cost);		
	}

}
